package com.yjps.house.service;

import com.yjps.house.dao.HistoryDao;
import com.yjps.house.dao.HouseDao;
import com.yjps.house.entity.HouseHistory;
import com.yjps.house.model.House;
import com.yjps.house.model.PublishHistory;
import com.yjps.system.dao.UserDao;
import com.yjps.system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("publishService")
public class PublishService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private UserDao userDao;

    /**
     * ���淿Դ(δ���)�ͷ�����¼
     * @param house
     * @param agentsId
     * @param publishTime
     * @return houseId
     */
    @Transactional
    public int publishHouse(House house,String agentsId,String publishTime) {
        //δ���״̬
        house.setState(0);
        //δ��֤
        house.setIsAuthentication(0);
        //�Ǿ�Ʒ
        house.setIsBoutique(0);
        house.setIsValid(1);
        int houseId = houseDao.save(house);

        //���淢����¼
        PublishHistory history = new PublishHistory(Integer.parseInt(agentsId), houseId, publishTime);
        history.setIsValid(1);
        int historyId = historyDao.save(history);

        return houseId;
    }

    /**
     * ��ȡ�þ��������з�Դ������¼
     * @param agentsId
     * @return
     */
    @Transactional
    public List<HouseHistory> allListByAgentsId(int agentsId, int startRow, int pageSize){
        //��ѯ�����˷�Դ������¼
        String hql="from PublishHistory where agentsId =:agentsId and isValid=:valid";
        Map<String, Object> map = new HashMap<>();
        map.put("agentsId", agentsId);
        map.put("valid", 1);
        List<PublishHistory> histories=historyDao.find(hql,map);

        List<HouseHistory> ps=new ArrayList<>();
        //��װ��Publish

        Iterator<PublishHistory> iter = histories.iterator();
        while (iter.hasNext()) {
            PublishHistory ph=iter.next();
            House house=houseDao.get(ph.getHouseId());
            if (house.getIsValid()==0){
                iter.remove();
            }else if (house!=null){
                String hql2="from User u where u.id in (select userId from Agent a where a.id =:agentsId)";
                Map<String,Object> map2=new HashMap<>();
                map2.put("agentsId",agentsId);
                List<User> users=userDao.find(hql2, map2);
                if (!users.isEmpty()){
                    HouseHistory p=new HouseHistory(agentsId,users.get(0).getUsername(),ph.getTime(),house);
                    ps.add(p);
                }
            }
        }
        if (ps.size()<pageSize){
            return ps;
        }
        if ((ps.size())<=(pageSize+startRow)){
            return ps.subList(startRow-1,ps.size());
        }

        return ps.subList(startRow-1,startRow+pageSize-1);

    }

    /**
     * ��ȡ�þ����˴���˵ķ�Դ������¼
     * @param agentsId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> unVerfiyListByAgentsId(int agentsId,int startRow,int pageSize){
        List<HouseHistory> ps= allListByAgentsId(agentsId, startRow, pageSize);
        Iterator<HouseHistory> iter = ps.iterator();
        while (iter.hasNext()) {
            HouseHistory houseHistory=iter.next();
            if (houseHistory.getHouse().getState()== 0){
                iter.remove();
            }
        }
        return ps;
    }

    /**
     * ��ȡ�þ���������˵ķ�Դ������¼
     * @param agentsId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> verfiyListByAgentsId(int agentsId,int startRow,int pageSize){
        List<HouseHistory> ps= allListByAgentsId(agentsId, startRow, pageSize);
        Iterator<HouseHistory> iter = ps.iterator();
        while (iter.hasNext()) {
            HouseHistory houseHistory=iter.next();
            if (houseHistory.getHouse().getState()== 1){
                iter.remove();
            }
        }
        return ps;
    }

    /**
     * ȡ����Դ����
     * @param houseId
     */
    @Transactional
    public boolean cancelPublish(int houseId){

        String hql="update House h set h.isValid =0  where h.id = :id";
        Map<String,Object> params = new HashMap<>();
        params.put("id",houseId);
        int affectRows=houseDao.excute(hql,params);

        if (affectRows==0){
            return false;
        }

        House house=houseDao.get(houseId);
        if (house.getState()==1){
            return true;
        }

        String hql2="update PublishHistory p set p.isValid =0  where p.houseId = :houseId";
        Map<String,Object> params2 = new HashMap<>();
        params2.put("houseId",houseId);
        int affectRows2=houseDao.excute(hql2, params2);

        if (affectRows2==0){
            return false;
        }
        return true;
    }


    //--------����һ���ָ���------------
    //����Ϊ�ŵ꾭��


    /**
     * ��ȡ�ŵ������еķ�Դ������¼
     * @param shopId
     * @return
     */
    @Transactional
    public List<HouseHistory> allListByShopId(int shopId,int startRow,int pageSize){
        //��ѯ���ŵ������о����˵ķ�����¼
        String hql="from PublishHistory p " +
                "where p.agentsId in(select a.id from Agent a where a.shopId = :shopId) and p.isValid = :isValid";
        Map<String, Object> map = new HashMap<>();
        map.put("shopId",shopId);
        map.put("isValid",1);
        List<PublishHistory> histories=historyDao.find(hql,map);

        //��ѯ��¼��Ӧ�ķ�Դ
        List<HouseHistory> ps=new ArrayList<>();

        Iterator<PublishHistory> iter = histories.iterator();
        while (iter.hasNext()) {
            PublishHistory ph = iter.next();
            House house = houseDao.get(ph.getHouseId());
            if (house.getIsValid() == 0) {
                iter.remove();
            } else if (house != null) {
                String hql2 = "from User u where u.id in (select userId from Agent a where a.id =:agentsId )";
                Map<String, Object> map2 = new HashMap<>();
                map2.put("agentsId", ph.getAgentsId());
                List<User> users = userDao.find(hql2, map2);
                if (!users.isEmpty()) {
                    HouseHistory p = new HouseHistory(ph.getAgentsId(), users.get(0).getUsername(), ph.getTime(), house);
                    ps.add(p);
                }
            }
        }
        if (ps.size()<pageSize){
            return ps;
        }
        if ((ps.size())<=(pageSize+startRow)){
            return ps.subList(startRow-1,ps.size());
        }

        return ps.subList(startRow-1,startRow+pageSize-1);

    }


    /**
     * ��ȡ�ŵ���δͨ����˵ķ�Դ������¼
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> unVerfiyListByShopId(int shopId,int startRow,int pageSize){
        List<HouseHistory> ps=allListByShopId(shopId, startRow, pageSize);
        Iterator<HouseHistory> iter = ps.iterator();
        while (iter.hasNext()) {
            HouseHistory houseHistory=iter.next();
            if (houseHistory.getHouse().getState()== 1){
                iter.remove();
            }
        }

        return ps;
    }

    /**
     * ��ȡ�ŵ�����ͨ����˵ķ�Դ������¼
     * @param shopId
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<HouseHistory> verfiyListByShopId(int shopId,int startRow,int pageSize){
        List<HouseHistory> ps=allListByShopId(shopId, startRow, pageSize);
        Iterator<HouseHistory> iter = ps.iterator();
        while (iter.hasNext()) {
            HouseHistory houseHistory=iter.next();
            if (houseHistory.getHouse().getState()== 0){
                iter.remove();
            }
        }
        return ps;
    }

}
